package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysChannelMapper;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.service.ISysChannelService;
import com.ruoyi.common.core.text.Convert;

/**
 * 渠道Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@Service
public class SysChannelServiceImpl implements ISysChannelService
{
    @Autowired
    private SysChannelMapper sysChannelMapper;

    /**
     * 查询渠道
     * 
     * @param id 渠道主键
     * @return 渠道
     */
    @Override
    public SysChannel selectSysChannelById(Long id)
    {
        return sysChannelMapper.selectSysChannelById(id);
    }

    /**
     * 查询渠道列表
     * 
     * @param sysChannel 渠道
     * @return 渠道
     */
    @Override
    public List<SysChannel> selectSysChannelList(SysChannel sysChannel)
    {
        return sysChannelMapper.selectSysChannelList(sysChannel);
    }

    /**
     * 新增渠道
     * 
     * @param sysChannel 渠道
     * @return 结果
     */
    @Override
    public int insertSysChannel(SysChannel sysChannel)
    {
        sysChannel.setCreateTime(DateUtils.getNowDate());
        return sysChannelMapper.insertSysChannel(sysChannel);
    }

    /**
     * 修改渠道
     * 
     * @param sysChannel 渠道
     * @return 结果
     */
    @Override
    public int updateSysChannel(SysChannel sysChannel)
    {
        sysChannel.setUpdateTime(DateUtils.getNowDate());
        return sysChannelMapper.updateSysChannel(sysChannel);
    }

    /**
     * 批量删除渠道
     * 
     * @param ids 需要删除的渠道主键
     * @return 结果
     */
    @Override
    public int deleteSysChannelByIds(String ids)
    {
        return sysChannelMapper.deleteSysChannelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除渠道信息
     * 
     * @param id 渠道主键
     * @return 结果
     */
    @Override
    public int deleteSysChannelById(Long id)
    {
        return sysChannelMapper.deleteSysChannelById(id);
    }

    @Override
    public SysChannel checkChannelByCode(String code) {
        SysChannel sysChannel = sysChannelMapper.checkChannelByCode(code);
        return sysChannel;
    }


    /**
     * 获取下一个充值渠道
     * SysChannel search = new SysChannel();
     *         search.setIsLock(0);
     *         List<SysChannel> channelList = sysChannelMapper.selectSysChannelList(search);
     * @return
     */
    @Override
    public SysChannel getNextChannelObj(List<SysChannel> channelList,SysOrder sysOrder) {
        //最后一次充值渠道ID
        //Long lastRechargeChannelId = Long.valueOf(sysOrder.getRealRechargePlat());
        //已经用过的充值渠道
        String hasRechargePlat = sysOrder.getHasRechargePlat();
        Map<Long,String> hasRechargePlatMap = new HashMap<>();
        if(StrUtil.isNotEmpty(hasRechargePlat)){
           String[] strs = hasRechargePlat.split(",");
           for (String s:strs){
               hasRechargePlatMap.put(Long.valueOf(s),"a");
           }
        }

        //剔除已经调用过的渠道
        List<SysChannel> removeChannelList = new ArrayList<>();
        for (SysChannel channel:channelList){
            if(hasRechargePlatMap.containsKey(channel.getId()) || channel.getIsLock() == 1){
                removeChannelList.add(channel);
            }
        }
        if(removeChannelList.size()>0){
            channelList.removeAll(removeChannelList);
        }


        if(channelList.size()==0){
            return null;
        }

        SysChannel nextChannel =  channelList.get(0);
        long minSort = channelList.get(0).getSort();
        for (SysChannel channel:channelList){
            if(channel.getSort()<minSort){
                minSort = channel.getSort();
                nextChannel = channel;
            }
        }
        return nextChannel;
    }





}
